﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;
using System.Net;

namespace EnvirConfig
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        string originDB;
        string originIP;
        XElement configFile;
        public Window1()
        {
            InitializeComponent();
            configFile = XElement.Load("Configuration.xml");
            originDB = configFile.Element("DatabaseIP").Value;
            originIP = configFile.Element("ThisIP").Value;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            dbIP.Text = originDB;
            //kitchenIP.Text = originKitchen;
            IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress[] ipAddr = ipHost.AddressList;
            foreach (var t in ipAddr)
            {
                if (t.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    thisIP.Items.Add(t.ToString());
                }
            }
            thisIP.Items.Add("127.0.0.1");
            thisIP.SelectedItem = originIP;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void ButtonReset_Click(object sender, RoutedEventArgs e)
        {
            dbIP.Text = originDB;
            //kitchenIP.Text = originKitchen;
            thisIP.Text = originIP;
        }

        private void ButtonOK_Click(object sender, RoutedEventArgs e)
        {
            originDB = dbIP.Text;
            originIP = thisIP.Text;
            //originKitchen = kitchenIP.Text;
            configFile.Element("DatabaseIP").SetValue(originDB);
            configFile.Element("ThisIP").SetValue(originIP);
            configFile.Save("Configuration.xml");
            MessageBox.Show("配置修改成功");
        }
    }
}
